﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

/// <summary>
/// 简单帐表：销售考核成本插件
/// </summary>
/// <remarks>
/// 功能：
/// 创建人：刘雪丽
/// 创建日期：2024-6-19
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.CB.K3Cloud.Plugin
{
    [Description("销售考核成本插件"), HotUpdate]
    public class SaleKHCOSTRptPlugin : SysReportBaseService
    {
        public override void Initialize()
        {

            // 简单账表类型：基本简单账表
            ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            //标识报表是否调用BuilderReportSqlAndTempTable接口创建临时表
            IsCreateTempTableByPlugin = true;
            //不在ui设计列
            this.ReportProperty.IsUIDesignerColumns = false;
            ReportProperty.ReportName = new Kingdee.BOS.LocaleValue("销售考核表");
            // ReportProperty.IdentityFieldName = "FIDENTITYID";
            // 标识报表支持汇总
            this.ReportProperty.IsGroupSummary = true;
            //合并字段主键
           // this.ReportProperty.PrimaryKeyFieldName = "FPRODUCTID_FNUMBER";
            //将产成品的id替换成名称显示
            // this.ReportProperty.DspInsteadColumnsInfo.DefaultDspInsteadColumns.Add("faterMaterial", "faterMaterialName");
        }

        /// <summary>
        /// 设置报表头
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            ReportTitles titles = new ReportTitles();
            DynamicObject myfilter = filter.FilterParameter.CustomFilter;
           /* if (myfilter != null)
            {
                //年度
                string F_QLTK_year = myfilter["F_QLTK_year"].ToString();
                //期间
                string F_QLTK_month = myfilter["F_QLTK_month"].ToString();
                //组织
                string F_QLTK_org = myfilter["F_QLTK_org_id"].ToString();
                titles.AddTitle("F_QLTK_year", F_QLTK_year);
                titles.AddTitle("F_QLTK_month", F_QLTK_month);
                titles.AddTitle("F_QLTK_org", F_QLTK_org);
            }*/
            return titles;
        }

        /// <summary>
        /// 构造取数Sql，取数据填充到临时表：tableName
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="tableName"></param>
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            base.BuilderReportSqlAndTempTable(filter, tableName);
            //默认排序字段：需要从filter中取用户设置的排序字段
            //KSQL_SEQ: ROW_NUMBER() OVER(ORDER BY  {0} ) FIDENTITYID
            string sortStr = filter.FilterParameter.SortString;
            KSQL_SEQ = string.Format(KSQL_SEQ, sortStr.IsNullOrEmptyOrWhiteSpace() ? " FDate asc" : sortStr);
            string sql;
            //1、解析过滤框
            DynamicObject myfilter = filter.FilterParameter.CustomFilter;
            //年度
            string F_QLTK_year = myfilter["F_QLTK_year"].ToString();
            //月度
            string F_QLTK_month = myfilter["F_QLTK_month"].ToString();
            //组织
            string F_QLTK_org = myfilter["F_QLTK_org_id"].ToString();

            //2、执行查询操作
            sql =
            $@"EXEC [dbo].[dev_p_saleKHCOST] @tempFilterTable = N'{tableName}',
		    @KSQL_SEQ = N'{KSQL_SEQ}',@fyear={F_QLTK_year},@fmonth={F_QLTK_month},@forgid={F_QLTK_org}";

            DBUtils.Execute(Context, sql);
        }
    }
}
